<template>
  <div class="home">
    <el-carousel
      height="80vh"
      direction="vertical"
      :autoplay="false"
      trigger="click"
      indicator-position="none"
      ref="carousel"
      :loop="false"
      @change="onChange"
      @wheel="handlerSrcool"
    >
      <el-carousel-item
        v-for="(item, index) in data"
        :key="index"
        class="player-container"
        style="overflow-y: auto"
      >
        <vue3VideoPlay
          :ref="setItemRef"
          v-bind="options"
          :src="item.src"
          class="video"
          :title="item.title"
          poster="https://cdn.jsdelivr.net/gh/xdlumia/files/video-play/ironMan.jpg"
        />
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
// import video from "@/assets/video/test.mp4";
import { ElMessage } from "element-plus";

const options = reactive({
  width: "100%",
  height: "35em",
  color: "#fff",
  speedRate: ["0.75", "1.0", "1.25", "1.5", "2.0", "5.0", "10.0", "16.0"],
  autoPlay: false,
  loop: true,
  controlBtns: [
    "audioTrack",
    "speedRate",
    "volume",
    "setting",
    "pip",
    "pageFullScreen",
    "fullScreen",
  ],
});

const oldVideo = ref(0);
const carousel = ref();

const data = ref([
  {
    title: "title1",
    src: "",
    isPlaying: false,
  },
  {
    title: "title2",
    src: "",
    isPlaying: false,
  },
  {
    title: "title3",
    src: "",
    isPlaying: false,
  },
  {
    title: "title4",
    src: "",
    isPlaying: false,
  },
]);

const itemRefs = ref([]);

const setItemRef = (el) => {
  if (el) {
    itemRefs.value.push(el);
  }
};

const onChange = (e) => {
  itemRefs.value.forEach((item) => item.pause());
  const currentVideo = itemRefs.value[e];
  currentVideo.play();
  itemRefs.value[oldVideo.value].pause();
  oldVideo.value = e;
};

const handlerSrcool = (e) => {
  setTimeout(() => {
    e.preventDefault();
    let current = oldVideo.value;
    // 判断滚动方向
    if (e.deltaY > 0) {
      if (oldVideo.value == data.value.length) {
        ElMessage({
          message: "当前视频是最后一个视频",
          grouping: true,
          type: "warning",
        });
        return;
      }
      carousel.value.next();
      current += 1;
    } else {
      if (oldVideo.value == 0) {
        ElMessage({
          message: "当前视频是最后一个视频",
          grouping: true,
          type: "warning",
        });
        return;
      }
      carousel.value.prev();
      current -= 1;
    }
    if (current > data.value.length) {
      current = data.value.length - 1;
    } else if (current < 0) {
      current = 0;
    }
    onChange(current);
  }, 200);
};

onMounted(() => {});
</script>

<style scoped>
.home {
  min-height: calc(100vh - 100px);
  background: #181623;
  border-radius: 10px;
  padding: 30px;
  box-sizing: border-box;
}

.player-container {
  margin-bottom: 100px;
  width: 100%;
  height: auto;
}

.video {
  height: 100%;
}

#refPlayerWrap {
  height: 100%;
}
</style>
